package com.hello.booksale.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hello.booksale.entity.BookEntity;
import com.hello.booksale.entity.UserEntity;
import com.hello.booksale.service.BookService;
import com.hello.booksale.service.QiniuService;
import com.hello.booksale.service.UserService;
import com.hello.booksale.util.RemoteImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

@Slf4j
@RequestMapping("/admin")
@Controller
public class AdminController {
    @Resource
    private QiniuService qiniuService;

    @Resource
    private UserService userService;
    @Resource
    private BookService bookService;

    @GetMapping("/book/main")
    public String bookMain() {
        return "forward:/admin/main";
    }

    @GetMapping("/main")
    public String main() {
        return "forward:/admin/main/1";
    }

    @GetMapping("/main/{pageNum}")
    public String main(@PathVariable("pageNum") int pageNum, Model m) {
        PageHelper.startPage(pageNum, 5);
        List<BookEntity> list = bookService.selectBook();
        PageInfo<BookEntity> pageInfo = new PageInfo<>(list, 5);
        m.addAttribute("pageInfo", pageInfo);
        m.addAttribute("bookList", list);
        return "/admin/main";
    }

    @PostMapping("/select")
    public String select(@RequestParam("bookName") String bookName, Model m) {
        PageHelper.startPage(1, 5);
        List<BookEntity> list = bookService.selectBookByBookName(bookName);
        PageInfo<BookEntity> pageInfo = new PageInfo<>(list, 5);
        m.addAttribute("pageInfo", pageInfo);
        m.addAttribute("bookList", list);
        return "/admin/main";
    }

    @GetMapping("/book/edit")
    public String bookEdit() {
        return "/admin/book/edit";
    }

    @GetMapping("/book/add")
    public String bookAdd() {
        return "/book_add";
    }

    @GetMapping("/user/add")
    public String userAdd() {

        return "/admin/user/add";
    }

    @GetMapping("/user/edit")
    public String userEdit() {
        return "/admin/user/edit";
    }


    @GetMapping("/user/main")
    public String userMain()
    {
        return "forward:/admin/user/main/1";
    }
    @GetMapping("/user/main/{pageNum}")
    public String userMain(@PathVariable("pageNum") int pageNum, Model m) {
        PageHelper.startPage(pageNum, 5);
        List<UserEntity> list = userService.selectAllUsers();
        PageInfo<UserEntity> pageInfo = new PageInfo<>(list, 5);
        m.addAttribute("pageInfo", pageInfo);
        m.addAttribute("userList", list);
        return "/admin/user/main";
    }

    @PostMapping("/book/add")
    public String test(@RequestParam(name = "book-img-local") MultipartFile file, HttpServletRequest request) throws IOException {
        String isLocalImage = request.getParameter("is-local-image");
        String bookUrl = "";
        if (Objects.equals(isLocalImage, "option2")) {
            bookUrl = qiniuService.uploadFile(file.getInputStream());

        } else if (Objects.equals(isLocalImage, "option1")) {
            String img = request.getParameter("book-img-url");
            if (img != null) {
                InputStream inputStream = RemoteImageUtil.getImageStream(img);
                if (inputStream != null) {
                    bookUrl = qiniuService.uploadFile(inputStream);
                } else {
                    log.error("远程图片加载失败");
                }
            } else {
                log.error("获取图片url失败");
            }

        } else {
            return "请选择书籍图片进行提交";
        }
        String bookAuthor = request.getParameter("book-author");
        String bookName = request.getParameter("book-name");
        Double bookPrice = Double.parseDouble(request.getParameter("book-price"));
        String bookDetail = request.getParameter("book-detail");
        int bookStock = Integer.parseInt(request.getParameter("book-stock"));
        BookEntity book = new BookEntity(bookName, bookPrice, bookDetail, bookAuthor, 0, bookStock, bookUrl);
        log.info("图书信息为: {}", book);
        bookService.insertBook(book);
        return "redirect:/admin/book/add";
    }
}
